package com.college.dao;

import java.util.List;

import javax.management.relation.Role;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.college.domain.College;
import com.college.domain.Roles;
import com.college.interfaces.IRolesDAO;

@Repository
@Transactional
public class RolesDAO implements IRolesDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void deleteRoleeById(int id) {
		Roles role = getRolesById(id);
		sessionFactory.getCurrentSession().delete(role);
		
	}

	@Override
	public Roles getRolesById(int id) {
		
		return (Roles) sessionFactory.getCurrentSession().get(Roles.class, id);
	}

	@Override
	public List<Roles> getRolesByName(String rolename) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Roles.class);
		criteria.add(Restrictions.ilike("roleName", rolename+"%"));
		return criteria.list();
	}

	@Override
	public List<Roles> getRolesList() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Roles.class);
		return criteria.list();
	}

	@Override
	public int saveRole(Roles role) {
		return (Integer) sessionFactory.getCurrentSession().save(role);
	}

	@Override
	public int updateRole(Roles role) {
		return (Integer) sessionFactory.getCurrentSession().merge(role);
	}

	@Override
	public List<Roles> getRoleByUserId(Integer userId) {
		return sessionFactory.getCurrentSession().createQuery("from Roles where userId=:userId")
		.setInteger("userId", userId).list();
	}

	

}
